package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysCollectCard;
import cn.wizzer.app.sys.modules.services.SysCollectCardService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/card")
public class SysCollectCardController {

    @Inject
    @Reference
    private SysCollectCardService cardService;

    @At("/")
    @Ok("beetl:/platform/sys/card/index.html")
    @RequiresPermissions("sys.finance.card.list")
    public void index() {

    }

    /**
     * 查询银行卡列表（分页）
     *
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate    String[] 时间数组
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.card.list")
    public Object data(@Param("searchKeyword") String searchKeyword, @Param("searchDate") String[] searchDate, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        return Result.success().addData(cardService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }

    /**
     * 添加card
     *
     * @param card card
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("sys.finance.card.add")
    @SLog(tag = "新建card", msg = "card号:${args[0].no}")
    public Object add(@Param("..") SysCollectCard card) {
        cardService.add(card);
        return Result.success();
    }


    /**
     * 根据id查询银行卡信息
     *
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        return Result.success(cardService.fetch(id));
    }


    /**
     * 根据id启用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.card.edit")
    @SLog(tag = "启用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object enable(String id) {
        cardService.clearCache();
        cardService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id禁用银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.card.edit")
    @SLog(tag = "禁用银行卡", msg = "银行卡名:${args[1].getAttribute('name')}")
    public Object disable(String id) {
        cardService.clearCache();
        cardService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id删除银行卡
     *
     * @param id String
     * @return Object
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.card.delete")
    public Object delete(String id) {
        cardService.clearCache();
        cardService.vDelete(id);
        return Result.success();
    }
}
